package com.lancoo.dictionary;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.Environment;

import com.lancoo.dictionary.data.model.DicSearchBehavior;
import com.lancoo.dictionary.widget.globaltoast.ListenClipboardService;

import org.xutils.DbManager;
import org.xutils.DbManager.DaoConfig;
import org.xutils.DbManager.DbUpgradeListener;
import org.xutils.x;

import java.io.File;
import java.net.URI;

/**
 * 
 * Copyright (C) 2016 LANCOO. All Rights Reserved.
 *
 * @author 刘孟孟 2016年5月17日
 */
public class DictionaryHelper {

	public static String API_PATH;
	public static String RES_BASE_URL;
	public static boolean SHOW_BACK_BTN;


	public static void install(Application app){
		x.Ext.init(app);
		x.Ext.setDebug(false);
	}
	
	public static void setUp(String apiPath){
		setUp(apiPath, "", false,true);
	}
	public static void setUp(String apiPath,String resBaseUrl){
		setUp(apiPath, resBaseUrl, false,true);
	}

	public static void setUp(String apiPath,String resBaseUrl,boolean openSearchService,boolean showBackBtn) {
		API_PATH=apiPath+"/api/Resources/GetCourseware";
		RES_BASE_URL = resBaseUrl;
		SHOW_BACK_BTN = showBackBtn;
		if (openSearchService) {
			ListenClipboardService.start(x.app());
		}
	}

	public static DbManager getDbManager(){
		DbManager dbManager = null;
		try {
			dbManager = x.getDb(getXDaoConfig());
			if (dbManager==null){
				dbManager =x.getDb(null);
			}
		}catch (SQLiteException e){
			e.printStackTrace();
		}
		return dbManager;
	}

	public static DaoConfig getXDaoConfig() {
		DaoConfig daoConfig = new DaoConfig();
		daoConfig.setDbName("LancooDictionary.db")
				.setDbDir(createSDCardAppFile(x.app().getApplicationContext(), "/db")).setDbVersion(1)
				.setDbUpgradeListener(new DbUpgradeListener() {

					@Override
					public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
					}
				});
		return daoConfig;
	}

	/**
	 * 
	 * @return CreatedFile on SD card for this App
	 */
	public static File createSDCardAppFile(Context context, String name) {
		File file = null;
		file = new File(getSDCardAppPath(context), name);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/**
	 * 
	 * @return The externalAppPath
	 */
	public static String getSDCardAppPath(Context context) {
		return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
				+ context.getApplicationInfo().packageName;
	}
	
	public static String uriEncode(String uri){
		return URI.create(uri.replace(" ", "%20")).toASCIIString();
	}

	public static final String DIC_SEARCH_ACTION = "com.lancoo.dic.search.action";
	public static final String KEY_DIC_SEARCH_BEHAVIOR= "dicSearchBehavior";

	public static void sendDicSearchBroadcast(Context context, String searchWord, int dataSource, boolean hasResult){
		DicSearchBehavior dicSearchBehavior = new DicSearchBehavior();
		dicSearchBehavior.setSearchWord(searchWord);
		dicSearchBehavior.setDataSource(dataSource);
		dicSearchBehavior.setHasResult(hasResult);
		dicSearchBehavior.setSearchTime(System.currentTimeMillis());

		Intent intent = new Intent();
		intent.putExtra(KEY_DIC_SEARCH_BEHAVIOR,dicSearchBehavior);
		intent.setAction(DIC_SEARCH_ACTION);
		context.sendBroadcast(intent);
	}
}
